package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentHomeOtherMemberBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MaterialFilterJson;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberCentralizedEntity;
import com.sgb.member.entity.MemberCheckStateEntity;
import com.sgb.member.entity.MemberFindPackersEntity;
import com.sgb.member.entity.MemberGoodsListEntity;
import com.sgb.member.entity.MemberLeaseBean;
import com.sgb.member.entity.MemberLogisticsEntity;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.entity.MemberServiceListBean;
import com.sgb.member.entity.MemberTeamBean;
import com.sgb.member.entity.MemberTeamPersonBean;
import com.sgb.member.entity.MemberTenderBean;
import com.sgb.member.ui.adapter.HomeOtherMemberAdapter;
import com.sgb.member.ui.adapter.PersonCommonAdapter;
import com.sgb.member.view_model.HomeOtherMemberModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration2;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class HomeOtherMemberFragment extends AppLazyFragment {

    @Inject
    public HomeOtherMemberModel sModel;
    private FragmentHomeOtherMemberBinding mBinding;
    private HomeOtherMemberAdapter<MemberBean> mMemberAdapter;
    private HomeOtherMemberAdapter<MemberPersonBean> personCommonAdapter;
    private HomeOtherMemberAdapter<MemberGoodsListEntity> mGoodAdapter;
    private HomeOtherMemberAdapter<MemberCentralizedEntity> mCollectionXAdapter;
    private HomeOtherMemberAdapter<MemberCentralizedEntity> mCollectionGAdapter;
    private HomeOtherMemberAdapter<MemberCentralizedEntity> mCollectionFactoryAdapter;
    private HomeOtherMemberAdapter<MemberLeaseBean> mLeaseAdapter;
    private HomeOtherMemberAdapter<MemberTenderBean> mBiddingAdapter;
    private HomeOtherMemberAdapter<MemberFindPackersEntity> mTeamAdapter;
    private HomeOtherMemberAdapter<MemberFindPackersEntity> mTeamClassAdapter;
    private HomeOtherMemberAdapter<MemberTeamPersonBean> mTeamPersonAdapter;
    private HomeOtherMemberAdapter<MemberLogisticsEntity> mAdapter;//物流
    private HomeOtherMemberAdapter<MemberServiceListBean> mFuWuAdapter;//企业服务
    private List<CommonPopEntity> mTabData = new ArrayList<>();
    private List<CommonPopEntity> mTabData2 = new ArrayList<>();
    private List<CommonPopEntity> mTabData3 = new ArrayList<>();
    private String mNo = "";
    private String mNo11 = "";
    private String mNo12 = "";
    private String mOriNo = "";
    private int mCurPos;
    private CommonPopEntity mCurEntity;
    private int comType;
    private boolean noMoreData;

    public static HomeOtherMemberFragment newInstance(int position, CommonPopEntity entity) {
        HomeOtherMemberFragment fragment = new HomeOtherMemberFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putSerializable("ENTITY", entity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home_other_member, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    private String lon = "";
    private String lat = "";

    @Override
    protected void initView() {
        assert getArguments() != null;
        lon = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE, "");
        lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE, "");
        comType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        mCurPos = getArguments().getInt("INDEX", 0);
        mCurEntity = (CommonPopEntity) getArguments().getSerializable("ENTITY");
        assert mCurEntity != null;
        if ("需方会员".equals(mCurEntity.getTypeName())) {
            mCurPos = 0;
        } else if ("供方会员".equals(mCurEntity.getTypeName())) {
            mCurPos = 1;
        } else if ("会员商品".equals(mCurEntity.getTypeName())) {
            mCurPos = 2;
        } else if ("会员招采".equals(mCurEntity.getTypeName())) {
            mCurPos = 3;
        } else if ("会员集采".equals(mCurEntity.getTypeName())) {
            mCurPos = 4;
        } else if ("会员租赁".equals(mCurEntity.getTypeName())) {
            mCurPos = 5;
        } else if ("会员工队".equals(mCurEntity.getTypeName())) {
            mCurPos = 6;
        } else if ("会员物流".equals(mCurEntity.getTypeName())) {
            mCurPos = 7;
        } else if ("会员企服".equals(mCurEntity.getTypeName())) {
            mCurPos = 8;
        }
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setPullRefreshEnabled(false);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        if (getActivity() != null) {
            boolean hasTab = loadOtherOneTab();
            if (hasTab) {
                loadData(false);
            }
        }
    }

    public void loadData(boolean isLoadMore) {
        if (noMoreData && isLoadMore) {
            closeRecyclerRefreshing();
            return;
        }
        //isLoadMore = false 初始化Recyclerview  为true只加载更多
        if (!isLoadMore){
            initRecyclerAdapter();
        }
        comType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        sModel.loadOtherList(mCurPos, isLoadMore, mNo, mNo11, comType, lat, lon, mNo12);
    }

    public String getOtherOneNo(int tab) {
        String no = "";
        String name = mCurEntity.getPickerViewText();
        if ("会员商品".equals(name)) {
            if (tab == 0) { //会员商品第一个全部id默认为0，所以置空
                no = "";
            } else {
                no = mTabData.get(tab).getId();
            }
        } else if ("会员企服".equals(name)) {
            if (tab == 0) { //会员商品第一个全部id默认为0，所以置空
                no = "";
            } else {
                no = mTabData.get(tab).getId();
            }
        } else {
            no = mTabData.get(tab).getNo();
        }
        return no;
    }

    public String getOtherTwoNo(int tab) {
        String no = "";
        String name = mCurEntity.getPickerViewText();
        if ("会员商品".equals(name)) {
            if (tab == 0) { //会员商品第一个全部id默认为0，所以置空
                no = "";
            } else {
                no = mTabData2.get(tab).getId();
            }
        } else if ("会员企服".equals(name)) {
            if (tab == 0) { //会员商品第一个全部id默认为0，所以置空
                no = "";
            } else {
                no = mTabData2.get(tab).getId();
            }
        } else {
            no = mTabData2.get(tab).getNo();
        }
        return no;
    }

    public boolean loadOtherOneTab() {
        if (getActivity() != null) {
            if (mTabData == null || mTabData.size() == 0) {
                sModel.loadOtherOneTab(mCurPos);
                return false;
            }
            return true;
        }
        return false;
    }

    public void loadOtherTwoTab() {
        if (mCurPos == 5) { // 租赁
            CommonPopEntity entity = mTabData.get(mBinding.tabLayout.getSelectedTabPosition());
            List<CommonPopEntity> children = entity.getChildren();
            if (children != null && children.size() > 0) {
                if (entity.getNo().equals("3")) {
                    // 车辆租赁
                    List<CommonPopEntity> childrenCar = entity.getChildren().get(0).getChildren();
                    if (childrenCar != null && childrenCar.size() > 0) {
                        if (!childrenCar.get(0).getTypeName().equals("全部")) {
                            childrenCar.add(0, new CommonPopEntity("", "全部"));
                        }
                    }
                    updateOtherTwoTab(childrenCar);
                } else {
                    if (!children.get(0).getTypeName().equals("全部")) {
                        children.add(0, new CommonPopEntity("", "全部"));
                    }
                    updateOtherTwoTab(children);
                }
            } else {
                updateOtherTwoTab(new ArrayList<>());
            }

        } else if (mCurPos == 8) {
            CommonPopEntity entity2 = mTabData.get(mBinding.tabLayout.getSelectedTabPosition());
            List<CommonPopEntity> children2 = entity2.getChildren();
            if (children2.size() > 0) {
                if(!children2.get(0).getTypeName().equals("全部")){
                    children2.add(0, new CommonPopEntity("", "全部"));
                }
                updateOtherTwoTab(children2);
            } else {
                updateOtherTwoTab(new ArrayList<>());
            }
        } else {
            sModel.loadOtherTwoTab(mCurPos, mNo);
        }
    }



    public void updateOtherOneTab(List<CommonPopEntity> data) {
        mBinding.tabLayout.removeAllTabs();
        if (data != null && data.size() > 0) {
            mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            mBinding.tabLayout.setVisibility(View.VISIBLE);
            mTabData.clear();
            mTabData.addAll(data);
            for (int i = 0; i < mTabData.size(); i++) {
                mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText(mTabData.get(i).getTypeName()));
            }
            mBinding.tabLayout.setNormalColor(R.color.color_title_small);
            mBinding.tabLayout.addOnTabSelectedListener(mTabData, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mNo = getOtherOneNo(tab.getPosition());
                    if (!TextUtils.isEmpty(mNo)) {
                        loadOtherTwoTab();
                    } else {
                        updateOtherTwoTab(new ArrayList<>());
                    }
                }
            });
            mBinding.tabLayout.scrollTo(0, 0);
            mNo = getOtherOneNo(0);
        } else {
            mNo = "";
            mBinding.tabLayout.setVisibility(View.GONE);
        }
        loadData(false);
    }

    public void updateOtherTwoTab(List<CommonPopEntity> data) {
        mBinding.tabLayout3.setVisibility(View.GONE);
        if (data == null || data.size() == 0) {
            mBinding.tabLayout2.setVisibility(View.GONE);
        } else {
            mBinding.emptyView.setVisibility(View.GONE);
            mBinding.tabLayout2.removeAllTabs();
            mBinding.tabLayout2.clearOnTabSelectedListeners();
            mTabData2.clear();
            mTabData2.addAll(data);
            mBinding.tabLayout2.setVisibility(View.VISIBLE);
            mBinding.tabLayout2.setTabMode(TabLayout.MODE_SCROLLABLE);
            for (int i = 0; i < mTabData2.size(); i++) {
                mBinding.tabLayout2.addTab(mBinding.tabLayout2.newTab().setText(mTabData2.get(i).getTypeName()));
            }
            mBinding.tabLayout2.setNormalColor(R.color.color_tab_three);
            mBinding.tabLayout2.addOnTabSelectedListener(mTabData2, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mNo11 = getOtherTwoNo(tab.getPosition());
                    if ("会员租赁".equals(mCurEntity.getTypeName())) {
                        updateOtherThreeTab(new ArrayList<>());
                    }else{
                        if (!TextUtils.isEmpty(mNo11)) {
                           CommonPopEntity bean = mTabData2.get(tab.getPosition());
                           if (bean.getChildren() != null && bean.getChildren().size() > 0) {
                              if (!bean.getChildren().get(0).getTypeName().equals("全部")) {
                                   bean.getChildren().add(0, new CommonPopEntity("", "全部"));
                              }
                           }
                           updateOtherThreeTab(bean.getChildren());
                       } else {
                           updateOtherThreeTab(new ArrayList<>());
                        }
                    }
                }
            });
            mNo11 = getOtherTwoNo(0);
            mBinding.tabLayout2.scrollTo(0, 0);
        }
        loadData(false);
    }

    public void updateOtherThreeTab(List<CommonPopEntity> data) {
        mNo12 = "";
        if (data == null || data.size() == 0) {
            mBinding.tabLayout3.setVisibility(View.GONE);
        } else {
            mBinding.emptyView.setVisibility(View.GONE);
            mBinding.tabLayout3.removeAllTabs();
            mBinding.tabLayout3.clearOnTabSelectedListeners();
            mTabData3.clear();
            mTabData3.addAll(data);
            mBinding.tabLayout3.setVisibility(View.VISIBLE);
            mBinding.tabLayout3.setTabMode(TabLayout.MODE_SCROLLABLE);
            for (int i = 0; i < mTabData3.size(); i++) {
               TabLayout.Tab tab =  mBinding.tabLayout3.newTab();
                tab.setText(mTabData3.get(i).getTypeName());
                mBinding.tabLayout3.addTab(tab);
            }
            mBinding.tabLayout3.setNormalColor(R.color.color_tab_three);
            mBinding.tabLayout3.addOnTabSelectedListener(mTabData3, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mNo12 = mTabData3.get(tab.getPosition()).getNo();
                    if (mNo12 == null) {
                        mNo12 = mTabData3.get(tab.getPosition()).getId();
                    }
                    loadData(false);
                }
            });
            mBinding.tabLayout3.scrollTo(0, 0);
            mNo12 = mTabData3.get(0).getNo();
            if (mNo12 == null) {
                mNo12 = mTabData3.get(0).getId();
            }
        }
        loadData(false);
    }

    public void refreshTabData() {
        if (mTabData != null && mTabData.size() > 0) {
            mTabData.clear();
            mBinding.tabLayout.removeAllTabs();
            mBinding.tabLayout.clearOnTabSelectedListeners();
            mBinding.tabLayout2.removeAllTabs();
            mBinding.tabLayout2.clearOnTabSelectedListeners();
        }
        mBinding.tabLayout.setVisibility(View.GONE);
        mBinding.tabLayout2.setVisibility(View.GONE);
        loadOtherOneTab();
    }

    private void initRecyclerAdapter() {
        if (getActivity() == null) {
            return;
        }
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        switch (mCurPos) {
            case 0:
            case 1:
                // 供方会员
                // 需方会员
                try {
                    if (comType == 0){
                        if (personCommonAdapter == null) {
                            personCommonAdapter = new HomeOtherMemberAdapter<MemberPersonBean>(getActivity(), R.layout.item_person_enterprise, null);
                            personCommonAdapter.setOnItemClick(new AdapterOnItemClick<MemberPersonBean>() {
                                @Override
                                public void onItemClick(MemberPersonBean memberPersonBean, int position) {
                                    if (!TextUtils.isEmpty(memberPersonBean.companyNo)) {
                                        MemberConfig.getInstance().startWebsiteInfo(getActivity(), memberPersonBean.companyNo, 1);
                                    } else {
                                        MToast.showToast(getActivity(), "店铺No为空");
                                    }
                                }
                            });
                        }
                        mBinding.recyclerView.setAdapter(personCommonAdapter);
                    }else{
                        if (mMemberAdapter == null) {
                            mMemberAdapter = new HomeOtherMemberAdapter<MemberBean>(getActivity(), R.layout.item_member_enterprise, null);
                            mMemberAdapter.setOnItemClick(new AdapterOnItemClick<MemberBean>() {
                                @Override
                                public void onItemClick(MemberBean memberBean, int position) {
                                    if (!TextUtils.isEmpty(memberBean.getCompanyNo())) {
                                        MemberConfig.getInstance().startWebsiteInfo(getActivity(), memberBean.getCompanyNo(), 1);
                                    } else {
                                        MToast.showToast(getContext(), "店铺No为空");
                                    }
                                }
                            });

                        }
                        mBinding.recyclerView.setAdapter(mMemberAdapter);
                    }
                } catch (Exception e) {
                    MLog.d("HomeOtherMemberFragment", "e : " + e.getMessage());
                }
                break;
            case 2:
                // 会员商品
                if (mGoodAdapter == null) {
                    mGoodAdapter = new HomeOtherMemberAdapter<MemberGoodsListEntity>(getActivity(), R.layout.item_other_material_goods, null);
                    mGoodAdapter.setOnItemClick(new AdapterOnItemClick<MemberGoodsListEntity>() {
                        @Override
                        public void onItemClick(MemberGoodsListEntity goodsListEntity, int position) {
                            // 商砼 == 7
                            if ("7".equals(goodsListEntity.getCid1())) {
                                MaterialFilterJson filterJson = new MaterialFilterJson();
                                filterJson.setStrengthId(goodsListEntity.getSort());
//                                ConcreteDetailActivity2.start(getActivity(), goodsListEntity.getId(), filterJson);
                                MemberConfig.getInstance().startConcreteDetail(getActivity(), goodsListEntity.getId(), filterJson);
                            } else {
//                                ShopGoodsDetailActivity.start(getContext(), goodsListEntity.getId(), "", goodsListEntity.getSkuid());
                                MemberConfig.getInstance().startShopGoodsDetail(getContext(), goodsListEntity.getId(), "", goodsListEntity.getSkuid());
                            }
                        }
                    });

                }
                mBinding.recyclerView.setAdapter(mGoodAdapter);
                break;
            case 3:
                // 会员招采
                if (mBiddingAdapter == null) {
                    if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                        mBinding.recyclerView.removeItemDecorationAt(0);
                    }
                    mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                    mBiddingAdapter = new HomeOtherMemberAdapter<MemberTenderBean>(getActivity(), R.layout.item_other_tender, null);
                    mBiddingAdapter.setOnItemClick(new AdapterOnItemClick<MemberTenderBean>() {
                        @Override
                        public void onItemClick(MemberTenderBean memberTenderBean, int position) {
                            sModel.tenderItemClick(memberTenderBean);
                        }
                    });
                    mBiddingAdapter.onIChildClick(new HomeOtherMemberAdapter.IChildClick<MemberTenderBean>() {
                        @Override
                        public void onClick(MemberTenderBean memberTenderBean) {
                            sModel.tenderItemClick(memberTenderBean);
                        }
                    });
                }
                mBinding.recyclerView.setAdapter(mBiddingAdapter);
                if (!TextUtils.equals(mOriNo, mNo)) {
                    mOriNo = mNo;
                    mBinding.recyclerView.setAdapter(mBiddingAdapter);
                }
                break;
            case 4:
                // 会员集采
                int collectionIndex = TextUtils.isEmpty(mNo) ? 0 : Integer.parseInt(mNo);
                switch (collectionIndex) {
                    case 0:
                        if (mCollectionXAdapter == null) {
                            if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                                mBinding.recyclerView.removeItemDecorationAt(0);
                            }
                            mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                            mCollectionXAdapter = new HomeOtherMemberAdapter<MemberCentralizedEntity>(getActivity(), R.layout.item_other_centralized_mining, null);
                            mCollectionXAdapter.onIChildClick(new HomeOtherMemberAdapter.IChildClick<MemberCentralizedEntity>() {
                                @Override
                                public void onClick(MemberCentralizedEntity entity) {
                                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                                    String endUrl = "?id=" + entity.getNo() + "&type=0" + "&token=" + token;
                                    sModel.getUrlSetting("collection", endUrl);
                                }
                            });

                        }
                        mBinding.recyclerView.setAdapter(mCollectionXAdapter);
                        if (!TextUtils.equals(mOriNo, mNo)) {
                            mOriNo = mNo;
                            mBinding.recyclerView.setAdapter(mCollectionXAdapter);
                        }
                        break;
                    case 1:
                        if (mCollectionFactoryAdapter == null) {
                            if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                                mBinding.recyclerView.removeItemDecorationAt(0);
                            }
                            mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                            mCollectionFactoryAdapter = new HomeOtherMemberAdapter<MemberCentralizedEntity>(getActivity(), R.layout.item_other_centralized_factory, null);
                            mCollectionFactoryAdapter.onIChildClick(new HomeOtherMemberAdapter.IChildClick<MemberCentralizedEntity>() {
                                @Override
                                public void onClick(MemberCentralizedEntity entity) {
                                    sModel.getUrlSetting("Factory", "?id=" + entity.getNo());
                                }
                            });

                        }
                        mBinding.recyclerView.setAdapter(mCollectionFactoryAdapter);
                        if (!TextUtils.equals(mOriNo, mNo)) {
                            mOriNo = mNo;
                            mBinding.recyclerView.setAdapter(mCollectionFactoryAdapter);
                        }
                        break;
                    case 2:
                        if (mCollectionGAdapter == null) {
                            if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                                mBinding.recyclerView.removeItemDecorationAt(0);
                            }
                            mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                            mCollectionGAdapter = new HomeOtherMemberAdapter<MemberCentralizedEntity>(getActivity(), R.layout.item_other_centralized_mining, null);
                            mCollectionGAdapter.onIChildClick(new HomeOtherMemberAdapter.IChildClick<MemberCentralizedEntity>() {
                                @Override
                                public void onClick(MemberCentralizedEntity entity) {
                                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                                    String endUrl = "?id=" + entity.getNo() + "&type=0" + "&token=" + token;
                                    sModel.getUrlSetting("collection", endUrl);
                                }
                            });

                        }
                        mBinding.recyclerView.setAdapter(mCollectionGAdapter);
                        if (!TextUtils.equals(mOriNo, mNo)) {
                            mOriNo = mNo;
                            mBinding.recyclerView.setAdapter(mCollectionGAdapter);
                        }
                        break;
                }
                break;
            case 5:
                // 会员租赁
                mBinding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                if (mLeaseAdapter == null) {
                    if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                        mBinding.recyclerView.removeItemDecorationAt(0);
                    }
                    mBinding.recyclerView.addItemDecoration(new RecyclerViewItemDecoration2(getResources().getDimension(R.dimen.dp10), getResources().getDimension(R.dimen.dp10)));
                    mLeaseAdapter = new HomeOtherMemberAdapter<MemberLeaseBean>(getActivity(), R.layout.item_other_lease, null);
                    mLeaseAdapter.setOnItemClick(new AdapterOnItemClick<MemberLeaseBean>() {
                        @Override
                        public void onItemClick(MemberLeaseBean entity, int position) {
                            sModel.getRentH5Url(entity.getType(), entity.getNo());
                        }
                    });

                }
                mBinding.recyclerView.setAdapter(mLeaseAdapter);
                break;
            case 6:
                // 会员工队
                int teamIndex = TextUtils.isEmpty(mNo) ? 0 : Integer.parseInt(mNo);
                Log.i("xxx","teamIndex " +teamIndex);
                switch (teamIndex) {
                    case 0: // 施工队
                        if (mTeamAdapter == null) {
                            if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                                mBinding.recyclerView.removeItemDecorationAt(0);
                            }
                            mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                            mTeamAdapter = new HomeOtherMemberAdapter<MemberFindPackersEntity>(getActivity(), R.layout.item_other_team, null);
                            mTeamAdapter.setOnItemClick(new AdapterOnItemClick<MemberFindPackersEntity>() {
                                @Override
                                public void onItemClick(MemberFindPackersEntity findPackersEntity, int position) {
                                    sModel.getTeamH5Url(findPackersEntity.getNo());
                                }
                            });

                        }
                        mBinding.recyclerView.setAdapter(mTeamAdapter);
                        if (!TextUtils.equals(mOriNo, mNo)) {
                            mOriNo = mNo;
                            mBinding.recyclerView.setAdapter(mTeamAdapter);
                        }
                        break;
                    case 1: // 班组
                        if (mTeamClassAdapter == null) {
                            if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                                mBinding.recyclerView.removeItemDecorationAt(0);
                            }
                            mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                            mTeamClassAdapter = new HomeOtherMemberAdapter<MemberFindPackersEntity>(getActivity(), R.layout.item_other_team, null);
                            mTeamClassAdapter.setOnItemClick(new AdapterOnItemClick<MemberFindPackersEntity>() {
                                @Override
                                public void onItemClick(MemberFindPackersEntity findPackersEntity, int position) {
                                    sModel.getTeamH5Url(findPackersEntity.getNo());
                                }
                            });

                        }
                        mBinding.recyclerView.setAdapter(mTeamClassAdapter);
                        if (!TextUtils.equals(mOriNo, mNo)) {
                            mOriNo = mNo;
                            mBinding.recyclerView.setAdapter(mTeamClassAdapter);
                        }
                        break;
                    case 3: // 工人
                        if (mTeamPersonAdapter == null) {
                            if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                                mBinding.recyclerView.removeItemDecorationAt(0);
                            }
                            mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                            mTeamPersonAdapter = new HomeOtherMemberAdapter<MemberTeamPersonBean>(getActivity(), R.layout.item_other_team_person_2, null);
                            mTeamPersonAdapter.setOnItemClick(new AdapterOnItemClick<MemberTeamPersonBean>() {
                                @Override
                                public void onItemClick(MemberTeamPersonBean teamPriceItemEntity, int position) {
//                                    TeamOfferDetailActivity.start(getActivity(), teamPriceItemEntity.getNo());
                                    MemberConfig.getInstance().startTeamOfferDetail(getActivity(), teamPriceItemEntity.getNo());
                                }
                            });

                        }
                        mBinding.recyclerView.setAdapter(mTeamPersonAdapter);
                        if (!TextUtils.equals(mOriNo, mNo)) {
                            mOriNo = mNo;
                            mBinding.recyclerView.setAdapter(mTeamPersonAdapter);
                        }
                        break;
                }
                break;
            case 7:
                mAdapter = new HomeOtherMemberAdapter<MemberLogisticsEntity>(getActivity(), R.layout.item_other_logistics, null);
                if (mAdapter == null) {
                    if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                        mBinding.recyclerView.removeItemDecorationAt(0);
                    }
                    mAdapter = new HomeOtherMemberAdapter<MemberLogisticsEntity>(getActivity(), R.layout.item_other_logistics, null);
                    mAdapter.setOnItemClick(new AdapterOnItemClick<MemberLogisticsEntity>() {
                        @Override
                        public void onItemClick(MemberLogisticsEntity goodsListEntity, int position) {
                            if (null != mAdapter && mAdapter.getDatas().size() > 0) {
                                sModel.goodsSourceStatusCheck(goodsListEntity.getNo(), position);
                            }
                        }
                    });
                }
                if (!TextUtils.equals(mOriNo, mNo)) {
                    mOriNo = mNo;
                }
                mBinding.recyclerView.setAdapter(mAdapter);
                break;
            case 8:
                if (mFuWuAdapter == null) {
                    if (mBinding.recyclerView.getItemDecorationCount() > 0) {
                        mBinding.recyclerView.removeItemDecorationAt(0);
                    }
                    mBinding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getResources().getDimension(R.dimen.dp1)));
                    mFuWuAdapter = new HomeOtherMemberAdapter<MemberServiceListBean>(getActivity(), R.layout.item_other_enterprise_service, null);
                    mFuWuAdapter.setOnItemClick(new AdapterOnItemClick<MemberServiceListBean>() {
                        @Override
                        public void onItemClick(MemberServiceListBean bean, int position) {
                            MemberConfig.getInstance().startWebsiteInfo(getContext(), bean.getNo(), MemberConfig.WEBSITE_FOR_COMPANY);
                        }
                    });

                }
                mBinding.recyclerView.setAdapter(mFuWuAdapter);
                if (!TextUtils.equals(mOriNo, mNo)) {
                    mOriNo = mNo;
                    mBinding.recyclerView.setAdapter(mFuWuAdapter);
                }
                break;
            default:
                break;
        }
    }

    public void showEmpty() {
        mBinding.recyclerView.setVisibility(View.INVISIBLE);
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
        Fragment parentFragment = getParentFragment();
        if (parentFragment instanceof HomeMemberFragment) {
            ((HomeMemberFragment) parentFragment).closeRecyclerRefreshing();
        }
    }

    public <T> void refreshRecycler(List<T> list, boolean isLoadMore, boolean noMoreData) {
        switch (mCurPos) {
            case 0:
            case 1:
                if (comType == 0){
                    // 供方会员
                    // 需方会员
                    refreshRecycler(personCommonAdapter, list, isLoadMore, noMoreData);
                }else {
                    // 供方会员
                    // 需方会员
                    refreshRecycler(mMemberAdapter, list, isLoadMore, noMoreData);
                }
                break;
            case 2:
                // 会员商品
                refreshRecycler(mGoodAdapter, list, isLoadMore, noMoreData);
                break;
            case 3:
                // 会员招采
                refreshRecycler(mBiddingAdapter, list, isLoadMore, noMoreData);
                break;
            case 4:
                // 会员集采
                int collectionIndex = TextUtils.isEmpty(mNo) ? 0 : Integer.parseInt(mNo);
                switch (collectionIndex) {
                    case 0:
                        refreshRecycler(mCollectionXAdapter, list, isLoadMore, noMoreData);
                        break;
                    case 1:
                        refreshRecycler(mCollectionFactoryAdapter, list, isLoadMore, noMoreData);
                        break;
                    case 2:
                        refreshRecycler(mCollectionGAdapter, list, isLoadMore, noMoreData);
                        break;
                }
                break;
            case 5:
                // 会员租赁
                refreshRecycler(mLeaseAdapter, list, isLoadMore, noMoreData);
                break;
            case 6:
                // 会员工队
                int teamIndex = TextUtils.isEmpty(mNo) ? 0 : Integer.parseInt(mNo);
                switch (teamIndex) {
                    case 0: // 施工队
                        refreshRecycler(mTeamAdapter, list, isLoadMore, noMoreData);
                        break;
                    case 1: // 班组
                        refreshRecycler(mTeamClassAdapter, list, isLoadMore, noMoreData);
                        break;
                    case 3: // 工人
                        refreshRecycler(mTeamPersonAdapter, list, isLoadMore, noMoreData);
                        break;
                }
                break;
            case 7:
                // 会员物流
                refreshRecycler(mAdapter, list, isLoadMore, noMoreData);
                break;
            case 8:
                // 会员企服
                refreshRecycler(mFuWuAdapter, list, isLoadMore, noMoreData);
                break;
            default:
                break;
        }
    }

    public <T> void refreshRecycler(HomeOtherMemberAdapter adapter, List<T> list, boolean isLoadMore, boolean noMoreData) {
       try{
           closeRecyclerRefreshing();
           if (adapter == null) {
               return;
           }
           mBinding.recyclerView.setVisibility(View.VISIBLE);
           mBinding.recyclerView.setLoadingMoreEnabled(!noMoreData);
           this.noMoreData = noMoreData;
           if (isLoadMore) {
               if (list != null && list.size() > 0) {
                   adapter.addDatas(list);
                   mBinding.emptyView.setVisibility(View.GONE);
               }else{
                   mBinding.emptyView.setVisibility(View.VISIBLE);
               }
           } else {
               if (list != null && list.size() > 0) {
                   mBinding.emptyView.setVisibility(View.GONE);
                   adapter.setDatas(list);
               } else {
                   adapter.clearDatas();
                   mBinding.emptyView.setVisibility(View.VISIBLE);
               }
           }
       }catch (Exception e){
           MLog.e(e.toString());
       }
    }

    public void checkGoodsState(int position, MemberCheckStateEntity stateEntity) {
        if (stateEntity.goodsIsGone()) {
            new RoundDialog.Builder(getActivity())
                    .title("提示")
                    .content("该货源不存在，请选择其他货源接单")
                    .singleBtn(true)
                    .confirm("知道了")
                    .confirmCallback(v -> {
                        mAdapter.getDatas().remove(position);
                        mAdapter.notifyDataSetChanged();
                    })
                    .show();
        } else if (stateEntity.goodsIsReceipted()) {
            new RoundDialog.Builder(getActivity())
                    .title("提示")
                    .content("该货源已被接单，请选择其他货源接单")
                    .singleBtn(true)
                    .confirm("知道了")
                    .confirmCallback(v -> {
                        mAdapter.getDatas().remove(position);
                        mAdapter.notifyDataSetChanged();
                    })
                    .show();

        } else {
            if (null != mAdapter && mAdapter.getDatas().size() > 0) {
                if (mAdapter.getDatas().get(position).getPattern() == 1) {
                    MemberConfig.getInstance().startFreightPrjDetailActivity(getActivity(), mAdapter.getDatas().get(position).getNo(), false, false);
                } else {
                    MemberConfig.getInstance().startGeneralPrjDetailActivity(getActivity(), mAdapter.getDatas().get(position).getNo(), mAdapter.getDatas().get(position).getPattern(), false);
                }
            }
        }
    }


}
